/*
* @Author: Choisaaaa
* @Date: 2018/5/21  22:48
* 卖家注册
*/


require("./seller-register.css");
var _school = require("util/school/school.js");
require("page/common/nav-simple/nav-simple.js");
var _code = require("util/verification-code/verification-code.js");
var _seller = require("service/seller-service.js");
var _verifyCode = require("service/verifyCode-service.js")
var _mm = require("util/mm.js");
var  formError = {
    show : function(errMsg){
        $(".error-item").show().find(".err-msg").text(errMsg);
    },
    hide : function(){
        $(".error-item").hide().find(".err-msg").text("");
    }
}

var page = {
    data : {
        verifyCode : true,
    },
    init : function(){
        this.loadSelect();
        _code.loadCode();
        this.bindEvent();
    },
    loadSelect : function () {
        var provinceArray = "";
        var provicneSelectStr = "";
        var province = _school.province;
        for(var i=0,len=province.length;i<len;i++){
            provinceArray = province[i];
            provicneSelectStr = provicneSelectStr + "<option value='"+provinceArray[0]+"'>"+provinceArray[1]+"</option>"
        }
        $("#province").html(provicneSelectStr);

        var selectCity = $("#province").val();
        var citylist=_school.city[selectCity];
        var cityArray = "";
        var citySelectStr = "";
        for(var i=0,len=citylist.length;i<len;i++){
            cityArray = citylist[i];
            citySelectStr = citySelectStr + "<option value='"+cityArray[0]+"'>"+cityArray[1]+"</option>"
        }
        $("#city").html(citySelectStr);
        var selectschool = $("#city").val();
        var schoolUlStr = "";
        var schoolListStr = _school.school[selectschool];
        for(var i=0,len=_school.school.length;i<len;i++){
            schoolUlStr = schoolUlStr + "<option >"+schoolListStr[i][2]+"</option>";
        }
        schoolUlStr = schoolUlStr + "<option value='999'>其它</option>";
        $("#school").html(schoolUlStr);
    },
    bindEvent : function(){
        var _this = this;
        $("#verification-code-value").blur(function(){
            var codeValue = $.trim($(this).val());
            if(!codeValue){
                formError.show("验证码不能为空！");
                return;
            }
            _verifyCode.checkCode(codeValue,
                function (res){
                    _this.data.verifyCode = true;
                    formError.hide();
                    _code.changeCode();
                }, function (err) {
                    _code.changeCode();
                    formError.show(err.msg);

                })
        });
        //省切换事件
        $("#province").change(function(){
            var selectCity = $("#province").val();
            var citylist=_school.city[selectCity];
            var cityArray = "";
            var citySelectStr = "";
            if(citylist!=null){
                for(var i=0,len=citylist.length;i<len;i++){
                    cityArray = citylist[i];
                    citySelectStr = citySelectStr + "<option value='"+cityArray[0]+"'>"+cityArray[1]+"</option>"
                }
            }

            $("#city").html(citySelectStr);
            $("#school1").show();
            $("#school2").hide();
            var selectschool = $("#city").val();
            var schoolUlStr = "";
            var schoolListStr = _school.school[selectschool];
            for(var i=0,len=schoolListStr.length;i<len;i++){
                schoolUlStr = schoolUlStr + "<option >"+schoolListStr[i][2]+"</option>";
            }
            schoolUlStr = schoolUlStr + "<option value='999'>其它</option>";
            $("#school").html(schoolUlStr);
        });
        //切换城市事件
        $("#city").change(function(){
            $("#school1").show();
            $("#school2").hide();
            var selectschool = $("#city").val();
            var schoolUlStr = "";
            var schoolListStr = _school.school[selectschool];
            for(var i=0,len=schoolListStr.length;i<len;i++){
                schoolUlStr = schoolUlStr + "<option >"+schoolListStr[i][2]+"</option>";
            }
            schoolUlStr = schoolUlStr + "<option value='999'>其它</option>";
            $("#school").html(schoolUlStr);
        });
        $("#school").change(function(){
            if($("#school").val()=="999"){
                $("#school1").hide();
                $("#school2").show();
            }
        });

        $("#submit").click(function(){
            if(_this.data.verifyCode){
                _this.submit();
            }else{
                formError.show("验证码校验不通过");
            }

        });
        $(".user-content").keyup(function(e){
            if(e.keyCode === 13){
                _this.submit();
            }
        });
    },
    //提交表单
    submit : function(){
        var fromData = {
            sellerRealName : $.trim($("#seller-name").val()),
            sellerSchool : $.trim($("#school").val()),
            selfSchool : $.trim($("#self-school").val()),
            sellerSchoolNumber : $.trim($("#school-number").val()),
        };
        var validateResult = _seller.formValidate(fromData);
        if(validateResult.status){//验证通过
            _seller.register(fromData, function() {//请求成功的回调函数
                    window.location.href = "./result.html?type=seller-register";
                },function(err){//请求失败的回调函数
                    if(err.status === 1){
                        formError.show(err.msg); //显示错误信息
                        if(err.msg == "用户未登录"){
                            if(window.confirm("是否马上去登录？")){
                                _mm.doLogin();
                            }
                        }
                    }else{
                        console.error("-------注册时的异步请求失败： "+ err.msg) ;
                        formError.show("服务器异常！请稍后再试"); //显示错误信息
                    }
                }
            );
        }else{//验证失败
            formError.show(validateResult.msg); //错误提示
        }
    }

};
$(function(){
    page.init();
});